﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using Temp.Database;
using Temp.Model;

namespace Temp.Controller
{
    class NavigationController
    {
        private SQLiteFactory sqlf;
        //private List<Temp.Model.Navigation> naviList;
        private List<Navigation> naviList;
        private List<Temp.Model.Object> objList;
        private List<Character> charList;

        private SQLite.SQLiteConnection conn;

        public NavigationController()
        {
            sqlf = new SQLiteFactory();
        }

        //public List<Rectangle> getAllBlock(int idPlace)
        //{
        //    conn = sqlf.getConnection();
        //    naviList = (conn.Table<Navigation>().Where(n => n.idPlace == idPlace && n.type.Equals("Block"))).ToList();
        //    conn.Close();

        //    List<Rectangle> obsList = new List<Rectangle>();

        //    for (int i = 0; i < naviList.Count; i++)
        //        obsList.Add(new Rectangle(naviList.ElementAt(i).x, naviList.ElementAt(i).y,
        //            naviList.ElementAt(i).dimX, naviList.ElementAt(i).dimY));

        //    return obsList;
        //}

        public List<Rectangle> getObstacles(int idPlace)
        {
            conn = sqlf.getConnection();
            naviList = (conn.Table<Navigation>().Where(n => n.idPlace == idPlace && n.type.Equals("Block"))).ToList();
            //objList = (conn.Table<Temp.Model.Object>().Where(n => n.idPlace == idPlace)).ToList();
            //charList = (conn.Table<Character>().Where(n => n.idPlace == idPlace)).ToList();
            conn.Close();

            List<Rectangle> obsList = new List<Rectangle>();

            for (int i = 0; i < naviList.Count; i++)
                obsList.Add(new Rectangle(naviList.ElementAt(i).x, naviList.ElementAt(i).y,
                    naviList.ElementAt(i).dimX, naviList.ElementAt(i).dimY));

            //note to self uncomment this part once fixed - sash
            //for (int i = 0; i < objList.Count; i++)
            //    obsList.Add(new Rectangle(objList.ElementAt(i).x, objList.ElementAt(i).y,
            //        objList.ElementAt(i).dimX, objList.ElementAt(i).dimY/2));

            //note to self uncomment this part once fixed - sash
            //for (int i = 0; i < charList.Count; i++)
            //    obsList.Add(new Rectangle(charList.ElementAt(i).x, charList.ElementAt(i).y,
            //        charList.ElementAt(i).Width, charList.ElementAt(i).Height/2));

            return obsList;
        }

        public List<Navigation> getAllMainBlocks(int idPlace)
        {
            List<Navigation> navis = new List<Navigation>();
            conn = sqlf.getConnection();
            navis = conn.Query<Navigation>("Select * from Navigation where idPlace=" + idPlace +" and type='Main'").ToList();
            conn.Close();

            return navis;
        }

        public List<Navigation> getAllBlocks(int idPlace)
        {
            List<Navigation> navis = new List<Navigation>();
            conn = sqlf.getConnection();
            navis = conn.Query<Navigation>("Select * from Navigation where idPlace=" + idPlace + " and type='Block'").ToList();
            conn.Close();

            return navis;
        }
        public Navigation getMainBlock(int idPlace)
        {
            Navigation nav;
            conn = sqlf.getConnection();
            nav = (conn.Table<Navigation>().Where(n => n.idPlace == idPlace && n.type.Equals("Main"))).Single();
            conn.Close();
            return nav;
        }
        public List<Navigation> getAllUpSideBlocks(int idPlace)
        {
            List<Navigation> navis = new List<Navigation>();
            conn = sqlf.getConnection();
            navis = conn.Query<Navigation>("Select * from Navigation where idPlace=" + idPlace + " and type='UpSideBlock'").ToList();
            conn.Close();

            return navis;
        }

        public List<Navigation> getAllRightSideBlocks(int idPlace)
        {
            List<Navigation> navis = new List<Navigation>();
            conn = sqlf.getConnection();
            navis = conn.Query<Navigation>("Select * from Navigation where idPlace=" + idPlace + " and type='RightSideBlock'").ToList();
            conn.Close();

            return navis;
        }
    }
}
